package com.rskj.cashbook.service.iml;

import com.rskj.cashbook.dao.DictDao;
import com.rskj.cashbook.service.IDictService;
import com.rskj.cashbook.util.SnowflakeIdWorker;
import com.rskj.cashbook.vo.Dict;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 字典服务实现类
 */
@Service
@RequiredArgsConstructor
@Transactional
public class DictService implements IDictService {
    
    private final DictDao dictDao;
    
    @Override
    public List<Dict> getByCategory(String category) {
        return dictDao.selectByCategory(category);
    }
    
    @Override
    public List<Dict> getByCategoryAndParent(String category, String parentId) {
        return dictDao.selectByCategoryAndParent(category, parentId);
    }
    
    @Override
    public Dict getById(String id) {
        return dictDao.selectById(id);
    }
    
    @Override
    public Dict getByCode(String code) {
        return dictDao.selectByCode(code);
    }
    
    @Override
    public int save(Dict dict) {
        if (dict.getId() == null) {
            dict.setId(SnowflakeIdWorker.getId());
        }
        return dictDao.insert(dict);
    }
    
    @Override
    public int update(Dict dict) {
        return dictDao.update(dict);
    }
    
    @Override
    public int delete(String id) {
        return dictDao.deleteById(id);
    }
    
    @Override
    public List<Dict> getAllEnabled() {
        return dictDao.selectAllEnabled();
    }
}
